﻿using System;
using System.Collections.Generic;

namespace PredatorPrey.Library.Algorithms
{
	// Id		: $Id: Search.cs 39 2009-04-23 21:38:07Z kristof@kristofmattei.be $
	// Date		: $Date: 2009-04-23 21:38:07 +0000 (Thu, 23 Apr 2009) $
	// Author	: $Author: kristof@kristofmattei.be $
	// Revision	: $Revision: 39 $
	/// <summary>
	/// Class which is the 'mother' of all search classes
	/// </summary>
	public abstract class Search
	{
		/// <summary>
		/// The Field
		/// </summary>
		public Field Field { get; protected set; }

		/// <summary>
		/// The constructor
		/// </summary>
		/// <param name="field">The field to</param>
		public Search(Field field)
		{
			if (null == field)
			{
				throw new ArgumentNullException("Field cannot be null");
			}

			this.Field = field;
		}

		/// <summary>
		/// This is where the actual algoritm should be implemented
		/// </summary>
		/// <returns>The actual path</returns>
		public abstract List<Cell> SearchPath(Cell start, Cell goal);

		/// <summary>
		/// Reconstructs the path
		/// </summary>
		/// <param name="cameFrom">A dict of cells containing where each cell came from</param>
		/// <param name="currentNode">The node we are currently at</param>
		/// <returns>A cleared out list to describe the path</returns>
		protected internal List<Cell> ReconstructPath(Dictionary<Cell, Cell> cameFrom, Cell currentNode)
		{
			if (cameFrom.ContainsKey(currentNode))
			{
				List<Cell> tempList = new List<Cell>();
				this.ReconstructPath(cameFrom, cameFrom[currentNode]).ForEach((item) => tempList.Add(item));
				tempList.Add(currentNode);
				return tempList;
			}
			else
			{
				return new List<Cell>();
			}
		}
	}
}
